/*
 * commons is a based project implemented
 * Copyright (C) 2024 Jasmine
 */
package com.easy.database.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import com.easy.core.constant.DynamicTableName;
import com.easy.database.handler.DynamicEntityTableNameHandler;

/**
 * Mybatis-Plus配置
 *
 * @author Jasmine
 * @since 1.0.0
 */
@Configuration
public class MybatisPlusAutoConfiguration {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        // 动态表名 以拦截器的方式处理表名称
        DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
        dynamicTableNameInnerInterceptor
            .setTableNameHandler(new DynamicEntityTableNameHandler(DynamicTableName.tableNameList));
        interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);

        // 防全表更新与删除操作
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());

        return interceptor;
    }
}
